home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
QRZ! Ham Radio 8
/
QRZ Ham Radio Callsign Database - Volume 8.iso
/
mac
/
files
/
infodata
/
callbook.tar
/
callbook_1.3
/
filter.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-07-17
|
2KB
|
125 lines
/*
* Telnet callsign server sources copyright 1989 by Devon Bowen, KA2NRC.
* You may distribute and modify these files as you please as long as
* as long as credit to the original creator is given. Please report any
* bug fixes or modification to bowen@cs.buffalo.edu.
*/
#include <stdio.h>
#include <regexp.h>
char *param(), *malloc(), *index();
struct filttype {
char option;
int index;
char *str;
regexp *expr;
} filt[] = {
{ 'c', 0, NULL, NULL },
{ 'l', 1, NULL, NULL },
{ 'f', 3, NULL, NULL },
{ 'a', 8, NULL, NULL },
{ 't', 9, NULL, NULL },
{ 's', 10, NULL, NULL },
{ 'z', 11, NULL, NULL },
{ ' ', 0, NULL, NULL }
};
filtsetup(cmd)
char *cmd;
{
char *reg;
int maxnum, cnt, cnt2;
for (cnt=0; filt[cnt].option != ' '; cnt++) {
if (filt[cnt].str != NULL) {
free(filt[cnt].str);
filt[cnt].str = NULL;
}
if (filt[cnt].expr != NULL) {
free(filt[cnt].expr);
filt[cnt].expr = NULL;
}
}
maxnum = paramnum(cmd);
for (cnt=1; cnt < maxnum; cnt++) {
char *field;
field = param(cmd, cnt);
if (*field == '-' && *(field+2) == '\0') {
for (cnt2=0; filt[cnt2].option != ' '; cnt2++)
if (filt[cnt2].option == *(field+1)) {
if (filt[cnt2].str != NULL) {
tputs("Duplicate filter\n", 0);
return 1;
}
reg = param(cmd, cnt+1);
if (reg == NULL) {
tputs("Bad filter param\n", 0);
return 1;
}
filt[cnt2].str = malloc(strlen(reg)+1);
strcasecpy(filt[cnt2].str, reg);
filt[cnt2].expr=regcomp(filt[cnt2].str);
if (filt[cnt2].expr == NULL) {
tputs("Bad expression\n", 0);
return 1;
}
cnt++;
break;
}
if (filt[cnt2].option == ' ') {
tputs("Bad filter\n", 0);
return 1;
}
}
}
return 0;
}
filter(data)
char *data;
{
int cnt;
char *fields[19];
char locbuf[BUFSIZ];
strcpy(locbuf, data);
fields[0] = locbuf;
for (cnt=1; cnt<19; cnt++) {
fields[cnt] = index(fields[cnt-1], '|');
*(fields[cnt]++) = '\0';
}
for (cnt=0; filt[cnt].option != ' '; cnt++) {
if (filt[cnt].str != NULL && !regexec(filt[cnt].expr,
fields[filt[cnt].index]))
return 0;
}
return 1;
}